package com.yunhai.store.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yunhai.common.dto.GlobalException;
import com.yunhai.common.dto.PaginationVo;
import com.yunhai.common.utils.ObjectUtil;
import com.yunhai.common.utils.ReflectionCopy;
import com.yunhai.model.entity.tms.StoreGroup;
import com.yunhai.model.vo.tms.StoreGroupAdd;
import com.yunhai.model.vo.tms.StoreGroupQuery;
import com.yunhai.model.vo.tms.StoreGroupUpdate;
import com.yunhai.store.mapper.StoreGroupMapper;
import com.yunhai.store.service.StoreGroupService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class StoreGroupServiceImpl implements StoreGroupService {

        @Resource
        private StoreGroupMapper storeGroupMapper;

        /**
         * 获取 StoreGroup 分页列表
         */
        @Transactional
        public IPage getStoreGroupPagination(PaginationVo paginationVo, StoreGroupQuery storeGroupQuery ) throws Exception {

            IPage page = new Page( paginationVo.getPageIndex(), paginationVo.getPageSize() );
            LambdaQueryWrapper<StoreGroup> lqs = new LambdaQueryWrapper();
            storeGroupMapper.selectPage(page, lqs);

            List<StoreGroup> record = page.getRecords();
            if( !ObjectUtil.isNotEmptyList(record)){
                return page;
            }
            return page;
        }


         @Transactional
        public List<StoreGroup> getStoreGroupAllList(StoreGroupQuery storeGroupQuery) throws Exception {

            LambdaQueryWrapper<StoreGroup> lqs = new LambdaQueryWrapper();
            List<StoreGroup> list = storeGroupMapper.selectList(lqs);
            return list;
        }


    /**
         * 获取 StoreGroup 详情
        */
      @Transactional
       public StoreGroup getStoreGroupDetail(Long id) throws Exception {

          StoreGroup storeGroup = storeGroupMapper.selectById(id);
          return storeGroup;
       }


        /**
         * 新增 StoreGroup
         * */
        @Transactional
        public void addStoreGroup(StoreGroupAdd storeGroupAdd) throws Exception {

            StoreGroup storeGroup = new StoreGroup();
            ReflectionCopy.copyProperties( storeGroup, storeGroupAdd );
            storeGroup.setGroupAmount(0);
            storeGroupMapper.insert(storeGroup);
            return;
        }


        /**
         * 更新 StoreGroup
         * */
        @Transactional
        public void updateStoreGroup( StoreGroupUpdate storeGroupUpdate) throws Exception {

            if( storeGroupUpdate.getGroupId() == null ){
                throw GlobalException.gen(10021, "门店组不存在");
            }
            StoreGroup storeGroup = new StoreGroup();
            ReflectionCopy.copyProperties( storeGroup, storeGroupUpdate );
            storeGroupMapper.updateById(storeGroup);
            return;
        }

        /**
         * 删除 StoreGroup
         * */
        @Transactional
        public void batchRemoveStoreGroup( List<Long> ids ) throws Exception {

            if( !ObjectUtil.isNotEmptyList(ids) ){
                throw GlobalException.gen(10011, "要删除的id不存在");
            }
            storeGroupMapper.deleteBatchIds(ids);
            return;
        }

    }

      